﻿using System;
using System.Collections.Generic;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统序列服务
    /// </summary>
    public class SeqService : BaseService<SysSeq>
    {
        public static readonly SeqService Instance = new SeqService();
        protected override string RootUrl => "/api/sys/seq/";

        /// <summary>
        /// 新建序列模型
        /// </summary>
        public SysSeq New()
        {
            return new SysSeq
            {
                Id = NewId(),
                StartWith = 1,
                CurrentValue = 1,
                IncrementBy = 1
            };
        }

        /// <summary>
        /// 复制序列模型
        /// </summary>
        /// <param name="original">原模型</param>
        public SysSeq Copy(SysSeq original)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            var model = original?.Clone();
            model.Id = NewId();
            return model;
        }
        
        /// <summary>
        /// 检查序列编码是否存在
        /// </summary>
        /// <param name="code">序列编码</param>
        public bool ExistByCode(string code)
        {
            return ExistByCodeCore(code);
        }

        /// <summary>
        /// 新建序列
        /// </summary>
        /// <param name="model">序列模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysSeq model)
        {
            return InsertCore(model);
        }

        /// <summary>
        /// 修改序列
        /// </summary>
        /// <param name="model">序列模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Update(SysSeq model)
        {
            return UpdateCore(model);
        }

        /// <summary>
        /// 删除序列
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string ids)
        {
            return DeleteByIdsCore(ids);
        }

        /// <summary>
        /// 根据主键获取序列对象
        /// </summary>
        /// <param name="id">序列主键</param>
        public SysSeq SelectById(string id)
        {
            return SelectByIdCore(id);
        }

        /// <summary>
        /// 根据序列编码获取序列对象
        /// </summary>
        /// <param name="code">序列编码</param>
        /// <returns>成功返回模型对象</returns>
        public SysSeq SelectByCode(string code)
        {
            return SelectByCodeCore(code);
        }

        /// <summary>
        /// 根据编码查询当前序列值
        /// </summary>
        /// <param name="code">序列编码</param>
        public int SelectCurrentValue(string code)
        {
            var url = GetUrl("selectCurrentValue", code);
            return SysRuntime.Api.Post<int>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 根据编码查询下一个序列值
        /// </summary>
        /// <param name="code">序列编码</param>
        public int SelectNextValue(string code)
        {
            var url = GetUrl("selectNextValue", code);
            return SysRuntime.Api.Post<int>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询序列分页列表
        /// </summary>
        /// <param name="paged">分页参数</param>
        /// <param name="key">编码/名称关键字</param>
        public PageList<SysSeq> SelectPageList(PageParam paged, string key)
        {
            var map = ToPageMap(paged).Add("key", key);
            return SelectPageListCore(map);
        }

        /// <summary>
        /// 查询序列列表
        /// </summary>
        /// <param name="key">编码/名称关键字</param>
        public List<SysSeq> SelectList(string key)
        {
            var map = Map.New.Add("key", key);
            return SelectListCore(map);
        }

        /// <summary>
        /// 导出序列列表
        /// </summary>
        /// <param name="key">编码/名称关键字</param>
        public byte[] Export(string key)
        {
            var map = Map.New.Add("key", key);
            return ExportCore(map);
        }
    }
}
